package redecolaborativa.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Button;

public class ItemAdquirido extends Composite {
	private FachadaAsync fachada;
	private AbsolutePanel absolutePanel;
	private String nomeItem, dono, dataInicial, idSessao;
	private int idEmprestimo;
	
	public ItemAdquirido(final FachadaAsync fachada, final String nomeItem, final String dono, final String dataInicial, final int idEmprestimo, final String idSessao) {
		this.nomeItem = nomeItem;
		this.dono = dono;
		this.dataInicial = dataInicial;
		this.idEmprestimo = idEmprestimo;
		this.idSessao = idSessao;
		this.fachada = fachada;
		
		absolutePanel = new AbsolutePanel();
		initWidget(absolutePanel);
		absolutePanel.setSize("619px", "92px");
		
		iniciaLabels();
		devolverItem();
	}

	private void devolverItem() {
		Button btnDevolverItem = new Button("Devolver Item");
		btnDevolverItem.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				try {
					fachada.devolverItem(idSessao, idEmprestimo, new AsyncCallback<Void>() {
						@Override
						public void onSuccess(Void result) {
							Window.alert("Notificao de devolucao do item foi enviada ao dono!");
						}
						@Override
						public void onFailure(Throwable caught) {
							Window.alert("Notificao de devolucao do item foi enviada ao dono!");
						}
					});
				} catch (Exception e) {
					Window.alert(e.getMessage());
				}
			}
		});
		absolutePanel.add(btnDevolverItem, 442, 36);
		btnDevolverItem.setSize("143px", "28px");
	}

	private void iniciaLabels() {
		Label lblItem = new Label("Item: ");
		lblItem.setStyleName("caption-panel");
		absolutePanel.add(lblItem, 10, 10);
		
		Label lblBeneficiado = new Label("Dono: ");
		lblBeneficiado.setStyleName("caption-panel");
		absolutePanel.add(lblBeneficiado, 10, 35);
		
		Label lblDataDeInicio = new Label("Data de inicio:");
		lblDataDeInicio.setStyleName("caption-panel");
		absolutePanel.add(lblDataDeInicio, 10, 60);
		
		Label lbNomeItem = new Label(nomeItem);
		absolutePanel.add(lbNomeItem, 50, 10);
		
		Label lbBeneficiado = new Label(dono);
		absolutePanel.add(lbBeneficiado, 60, 35);
		
		Label lbDataInicial = new Label(dataInicial);
		absolutePanel.add(lbDataInicial, 116, 60);
	}
}
